using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ltf.Remoting.Operations;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.Diffs.MergeCommands
{
	public class MergeRenameFileCommand : MergeCommand
	{
		public MergeRenameFileCommand(TFServerConnection connection, string targetFile, string newName)
		{
			this.Connection = connection;
			this.TargetFile = targetFile;
			this.newName = newName;
		}

		private string newName;

		public string NewName
		{
			get { return newName; }
			set { newName = value; }
		}

		public override MergeResult Execute()
		{
			WorkspaceOperators wso = GetWorkspaceOperators();
			if (wso != null)
			{
				wso.PendMove(TargetFile, newName);
			}
			else
			{
				File.Move(TargetFile, newName);
			}
			bool renamed = (Path.GetDirectoryName(TargetFile)==Path.GetDirectoryName(newName));
			return new MergeResult(0, renamed ? 1 : 0, renamed ? 0 : 1, 0, 0,0 );
		}

		public override void Validate()
		{
			if (!File.Exists(TargetFile))
				throw new MergeException(string.Format("Target file '{0}' does not exist.", TargetFile));
			if (File.Exists(NewName))
				throw new MergeException(string.Format("New file name'{0}' already exists.", NewName));
		}

		public override string ShortName
		{
			get { return "Rename"; }
		}
	}
}
